package com.sinothk.hm.snapshot.comm.view.slice;

import com.sinothk.hm.snapshot.MainAbility;
import com.sinothk.hm.snapshot.ResourceTable;
import com.sinothk.hm.snapshot.comm.model.base.ResultInfo;
import com.sinothk.hm.snapshot.comm.model.domain.LoginDto;
import com.sinothk.hm.snapshot.comm.viewModel.CommViewModel;
import com.sinothk.software.ui.base.TitleBaseLightAbilitySlice;
import com.sinothk.software.utils.XUtils;
import com.sinothk.software.views.RoundRectView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.InputStream;

public class LoginAbilitySlice extends TitleBaseLightAbilitySlice {

    private CommViewModel commViewModel;
    TextField phoneNumTf;
    TextField passwordTf;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);

        commViewModel = new CommViewModel();
    }

    /**
     * 通过资源ID获取位图对象
     **/
    private PixelMap getPixelMap(int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();

        DirectionalLayout rootView = (DirectionalLayout) findComponentById(ResourceTable.Id_rootView);
        rootView.setBackground(bg_root_view);

        DependentLayout imageView = (DependentLayout) findComponentById(ResourceTable.Id_imageView);
        RoundRectView rectView = new RoundRectView(this, new Size(220, 220));
        rectView.putPixelMap(getPixelMap(ResourceTable.Media_bg_welcome));

        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        imageView.addComponent(rectView, layoutConfig);

        phoneNumTf = (TextField) findComponentById(ResourceTable.Id_phoneNumTf);
        passwordTf = (TextField) findComponentById(ResourceTable.Id_passwordTf);
        Text okBtn = (Text) findComponentById(ResourceTable.Id_okBtn);
        okBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                XUtils.toast().show("登录操作");
                login();
            }
        });
    }

    private void login() {
        String phoneNum = phoneNumTf.getText();
        String password = passwordTf.getText();

        if (XUtils.string().isEmpty(phoneNum)) {
            XUtils.toast().show("手机号不能为空");
            return;
        }

        if (XUtils.string().isEmpty(password)) {
            XUtils.toast().show("密码不能为空");
            return;
        }

        commViewModel.login(phoneNum, password);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBusCallback(ResultInfo<LoginDto> result) {

        if (!"login".equals(result.getEventType())) {
            return;
        }

        if (ResultInfo.SUCCESS == result.getCode()) {
            XUtils.intent().open(getAbility(), MainAbility.class).finish(true).start();
        } else {
            XUtils.toast().show(result.getMsg());
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
